package com.example.gsnd21study.utils;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;


//Restful风格API接口统一响应格式。
@Getter
@Setter
public class Result<T> {
    /**
     *   code: 自定义响应状态码。
     */
    private Integer code;
    /**
     *   status: 自定义响应状态描述
     */
    private String Msg;
    /**
     *   data 默认响应携带数据
     */
    private T data;
    /**
     *  time  时间
     */
    private LocalDateTime Time;


    private Result(ResultCode resultCode , T data) {
        this.code = resultCode.getCode();
        this.Msg = resultCode.getMsg();
        this.data = data;
        this.Time = LocalDateTime.now();
    }

    public Result(){}

    public static <O> Result<O> success() {
        return success(null);
    }


    public static <O> Result<O> success(O data) {
       return fail(ResultCode.SUCCESS,data);
    }

    public static <O> Result<O> error(){
        return fail(ResultCode.ERROR , null);
    }

    public static <O> Result<O> fail(ResultCode resultCode , O data) {
        return new Result<O>(resultCode , data);
    }

   public static Result<?> CustomizeResultCode(Integer code , String msg) {
        Result<?> result = new Result<>();
        result.setCode(code);
        result.setMsg(msg);
        return result;
   }


}